home *** CD-ROM | disk | FTP | other *** search
/ Learn Microsoft Visual Basic 6.0 Now / Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO / media / chap09 / b09d005.cc2 < prev    next >
Text File  |  1998-06-07  |  5KB  |  109 lines

  1. 0, Do you remember the Lucky Seven program? 
  2. 4, In this demonstration, I'll revisit the 
  3. 6, Lucky Seven program to explore the 
  4. 8, concept of a public variable. I'll add a 
  5. 11, standard module to Lucky Seven and I'll 
  6. 13, create a public variable in it named Wins, 
  7. 16, which keeps track of the number of 
  8. 17, jackpots I hit when running the slot machine. 
  9. 21, Let's give it a try. Right now, each 
  10. 23, time I click the Spin button, a 7 appears, 
  11. 27, and a jackpot appears, but I have no 
  12. 29, real way of keeping track of my wins. So, 
  13. 32, I'll end the program, and I'll go down 
  14. 34, to the Lucky Seven label, and make it a 
  15. 37, little smaller since it takes up so much 
  16. 38, room. Below the Lucky Seven label, I'll 
  17. 41, create another label, called Wins. In 
  18. 44, this label, I'll keep track of the number 
  19. 46, of wins in my program. And I'll click 
  20. 50, the Properties window to set some 
  21. 51, properties for the Label5 object. I'll set the 
  22. 57, Name property to lblWins to identify it 
  23. 61, in the program code. And I'll set the 
  24. 65, Alignment property to Center. Next, I'll 
  25. 70, set the Caption property to Wins. And I'll 
  26. 75, set the Original Value to 0. And I'll 
  27. 79, use the Font property to specify Arial, 
  28. 84, 12-point, bold, italic. And I'll use the 
  29. 90, ForeColor property to specify a dark 
  30. 93, green color. Now, I'll change the caption 
  31. 100, on my form to Lucky Seven, a detail I 
  32. 106, didn't add in the first chapter, but which 
  33. 108, makes it look nice. Okay, now that I've 
  34. 113, finished creating my label on the form, 
  35. 115, I'm ready to add a standard module to 
  36. 117, the project. And I'll do that by clicking 
  37. 120, the Project menu and clicking the Add 
  38. 122, Module command. Visual Basic displays the 
  39. 128, Add Module dialog box. And when I click 
  40. 131, the Open button, Visual Basic adds a new 
  41. 135, module, named Module1, to my project, 
  42. 138, and it appears in the Code window. And I 
  43. 141, can declare my public variable, named 
  44. 143, Wins, by typing Public Wins in the code 
  45. 148, module. And Visual Basic will declare a 
  46. 151, variable named Wins that is of the variant 
  47. 155, type. That means it can include any 
  48. 156, type of information in the variable. We'll 
  49. 159, have an integer number, but it could be 
  50. 161, any type because it's a variant. And 
  51. 163, that will be a public variable available to 
  52. 165, all the event procedures in the 
  53. 167, program. So I can go ahead and close the 
  54. 171, Properties window and then open up the Project 
  55. 175, window. And as you can see, my new 
  56. 181, module appears in the Modules folder in the 
  57. 183, Project window. And when I want to save 
  58. 185, this module to disk, I just need to 
  59. 187, highlight that module and then use the Save 
  60. 190, Module As command on the File Menu. 
  61. 193, Okay, now I want to go back into the first 
  62. 195, form and add some code that's associated 
  63. 198, with that Spin button. So, I'll click 
  64. 200, the form and then click the View Code 
  65. 202, button. And when the Code window appears, I 
  66. 206, can begin to add the program code that 
  67. 207, increments my Wins variable. So I'll get 
  68. 210, rid of the Project window to add some 
  69. 212, more space. And immediately below the Beep 
  70. 215, statement, the place I want to 
  71. 216, increment my variable because I have a win, I 
  72. 219, can type the program code, Wins = Wins + 
  73. 224, 1. And this will increment the Wins 
  74. 225, variable every time I win. Then I'll go ahead 
  75. 228, and display that value. I'll type, 
  76. 230, lblWins.Caption, this is my new label on my 
  77. 235, form, = "Wins: " , which is added to the 
  78. 244, Wins variable. So this is the part of 
  79. 247, the program code that increments the Wins 
  80. 250, public variable when a 7 appears in a 
  81. 252, Spin. Now the second statement uses the 
  82. 254, concatenation operator to assign a 
  83. 257, string to the lblWins object. And that 
  84. 260, format is Wins:Number. Okay, and that will 
  85. 264, increment as my program goes. So now, 
  86. 267, let's run this and see what it looks like. 
  87. 270, Okay, I'll click the Start button. Now, I 
  88. 274, can really keep track of the Lucky 
  89. 275, Seven program. The first time I click the 
  90. 277, Spin button, I have a win, and a Win 
  91. 280, appears in the label I just added. The second 
  92. 283, time, I have a win. The third time, I 
  93. 285, have a win, the fourth time, and the 
  94. 287, fifth time I have a win. Then I go on a bit of a 
  95. 290, jag, but after a total of ten clicks, I 
  96. 293, have six wins. The public variable Wins 
  97. 296, was particularly useful here because it 
  98. 298, maintained its value through ten calls of 
  99. 300, the Command1_Click event procedure. Now, 
  100. 302, if I had declared Wins locally in that 
  101. 304, procedure, the variable would have reset 
  102. 306, each time. It's a bit like what a trip 
  103. 308, odometer in your car does when you push the 
  104. 310, reset button. But using a public 
  105. 312, variable in a standard module lets you avoid 
  106. 314, hitting the reset button. Public variables 
  107. 317, have more in common with the main 
  108. 319, odometer in your car.
  109. 320, END